<?php
/**
 * Created by PhpStorm.
 * User: hongtianjin
 * Date: 2017/3/1 0001
 * Time: 18:05
 */

namespace App\Http\Controllers\Web\ActivityPoint;

use App\App\Services\Api\PointService;
use App\App\Services\Web\UserService;
use App\App\Services\WechatService;
use App\Http\Controllers\Controller;
use App\Support\Helpers\StringHelper;
use Illuminate\Http\Request;
use App\Models\ActivityPoints\ActivityPointsService;

class ActivityPointController extends Controller
{
    public function index(Request $request)
    {
        $action = $request->get('action');
        return view('web.activityPoint.list', ['action'=>$action]);
    }
    /**
     * 获取埋点列表
     */
    public function getActivityPointsList(Request $request, ActivityPointsService $activityPointsService, UserService $userService)
    {
        $loginUser = $userService->getLoginUser();
        $user_id = $loginUser['id'];
        $action = $request->get('action');

        $data = $activityPointsService->getPointsListByPage($request, $user_id, $action);

        return $this->respondJson($data);
    }

    /**
     * @param Request $request
     * 新增活动埋点
     */
    public function addActivityPoints(Request $request, ActivityPointsService $activityPointsService, UserService $userService)
    {
        $this->validate($request, [
            'point_name'=>'required',
            'desc'=>'required',
        ], [
            'point_name.required'=>'请填写埋点名称',
            'desc.required'=>'请填写埋点描述',
        ]);
        $loginUser = $userService->getLoginUser();
        $data['point_name'] = $request->request->get('point_name');
        $data['desc'] = $request->request->get('desc');
        $data['point_key'] = md5(StringHelper::getRandChar(10).(microtime(true)*1000));
        $data['user_id'] = $loginUser['id'];
        $success = $activityPointsService->createPoints($data);

        if ($success) {
            return  $this->respondJson(['message'=>'添加成功']);
        }

        return  $this->respondJson(['message'=>'添加失败','code'=>1]);
    }

    /**
     * 编辑埋点位置
     * @param Request $request
     * @param ActivityPointsService $activityPointsService
     */
    public function editActivityPoints(Request $request, ActivityPointsService $activityPointsService, PointService $pointService)
    {
        $data['id'] = $request->request->get('id');
        $data['point_name'] = $request->request->get('point_name');
        $data['desc'] = $request->request->get('desc');

        $this->validate($request, [
            'id'=>'required',
            'point_name'=>'required',
            'desc'=>'required',
        ], [
            'id.required'=>'埋点ID参数错误',
            'point_name.required'=>'请填写埋点名称',
            'desc.required'=>'请填写埋点描述',
        ]);
        $res = $activityPointsService->modify($data, ['id'=>$data['id']]);
        $pointService->delCache($request->request->get('point_key'));   //刷新埋点缓存
        if ($res) {
            return  $this->respondJson(['message'=>'编辑成功']);
        }
        return  $this->respondJson(['message'=>'编辑失败','code'=>1]);
    }

    /**
     * 删除埋点
     * @param Request $request
     * @param ActivityPointsService $activityPointsService
     * @return \Illuminate\Http\JsonResponse
     */
    public function delActivityPoints(Request $request, ActivityPointsService $activityPointsService)
    {
        $ids = $request->input('ids');
        $this->validate($request, [
            'ids' =>'required'
        ], [
            'ids.required'=>'埋点ID不能为空'
        ]);
        $res = $activityPointsService->deleteActivityPoints($ids);
        if (!$res) {
            return  $this->respondJson(['message'=>'该埋点正在使用中，不可删除','code'=>1]);
        }
        return  $this->respondJson(['message'=>'删除成功']);
    }
}
